/*

 * Main.java

 *

 * Created on January 8, 2008, 11:28 AM

 *

 * To change this template, choose Tools | Template Manager

 * and open the template in the editor.

 */

package com.fie.newsfeed;

import com.sun.cnpi.rss.elements.Category;

import com.sun.cnpi.rss.elements.Item;

import com.sun.cnpi.rss.elements.Rss;

import com.sun.cnpi.rss.parser.RssParser;

import com.sun.cnpi.rss.parser.RssParserFactory;

import java.net.URL;

import java.util.Collection;

import java.util.Iterator;

import javax.swing.JLabel;



/**

 *

 * @author E11997

 */

public class RssFeed {

    private JLabel j;

    /** Creates a new instance of Main */

    public RssFeed() {

        j = new JLabel();

    }



    public  String readRSSDocument() throws Exception{

        String xm ="<html><body>";

        RssParser parser = RssParserFactory.createDefault();

        Rss rss = parser.parse(

                new URL("http://finance.yahoo.com/rss/topstories"));

        //Get all XML elements in the feed

         Collection items = rss.getChannel().getItems();

            if(items != null && !items.isEmpty())

            {

            //Iterate over our main elements. Should have one for each article

                for(Iterator i = items.iterator();

                    i.hasNext();

                    System.out.println())

                {

                    Item item = (Item)i.next();

                    System.out.println("Title: " + item.getTitle());

                   xm = xm + item.getTitle() + "<br>";

                   // System.out.println("Link: " + item.getLink());

                    System.out.println("Description: " + item.getDescription());

                }



            }

        //Iterate over categories if we are provided with any

            Collection categories = rss.getChannel().getCategories();

            if(categories != null && !categories.isEmpty())

            {

                Category cat;

                for(Iterator i = categories.iterator();

                    i.hasNext();

                    System.out.println("Category Domain: " + cat.getDomain()))

                {

                    cat = (Category)i.next();

                    System.out.println("Category: " + cat);

                }

            }

            xm = xm + "</body></html>";

              return xm;

          

}

   

     public  String readRSSDocumentTicker(String ticker) throws Exception{

    String xm ="<html><body>";

    RssParser parser = RssParserFactory.createDefault();

    Rss rss = parser.parse(

            new URL("http://finance.yahoo.com/rss/headline?s="+ticker));

    //Get all XML elements in the feed

     Collection items = rss.getChannel().getItems();

        if(items != null && !items.isEmpty())

        {

        //Iterate over our main elements. Should have one for each article
            int counter = 0;
            for(Iterator i = items.iterator(); i.hasNext(); System.out.println())

            {

                if(counter <5){
                    Item item = (Item)i.next();

                    System.out.println("Title: " + item.getTitle());

                   xm = xm + "<b>"+item.getTitle() + "</b><br>";

                   if(!item.getDescription().equals(" "))

                    xm = xm + item.getDescription() + "<br><br>";

                   // System.out.println("Link: " + item.getLink());

                    System.out.println("Description: " + item.getDescription());
                    counter++;
                }
                i.next();

            }

         

        }

    //Iterate over categories if we are provided with any

        Collection categories = rss.getChannel().getCategories();

        if(categories != null && !categories.isEmpty())

        {

            Category cat;

            for(Iterator i = categories.iterator();

                i.hasNext();

                System.out.println("Category Domain: " + cat.getDomain()))

            {

                cat = (Category)i.next();

                System.out.println("Category: " + cat);

            }

        }

        xm = xm + "</body></html>";

          return xm;

          

}

   

}